package com.open.server.api.system;

import com.open.base.dispatch.IGeneralConfigDispatch;
import com.open.base.model.GeneralConfig;
import com.open.common.model.Page;
import com.open.common.model.PageRequest;
import com.open.common.model.Result;
import com.open.base.model.dto.UserConfigDto;
import com.open.base.model.vo.SysConfigVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 通用配置
 *
 * @author 大树03
 * @date 2025-03-31 21:00
 */
@RestController
@RequestMapping("system/config")
public class GeneralConfigApi {

    @Autowired
    private IGeneralConfigDispatch generalConfigDispatch;

    /**
     * 对象添加
     * @param model 添加的对象
     * @return
     */
    @PostMapping("save")
    public Result <GeneralConfig> save(@RequestBody GeneralConfig model)  {
        long now=System.currentTimeMillis();
        Result<GeneralConfig> result=new Result<>();
        generalConfigDispatch.save(model);
        result.setData(model);
        result.setTime(System.currentTimeMillis()-now);
        return result;
    }

    /**
     * 批量对象修改
     * @param models 修改的对象
     * @return
     */
    @PostMapping("batchUpdate")
    public Result<Boolean> batchUpdate(@RequestBody List<GeneralConfig> models)  {
        long now=System.currentTimeMillis();
        Result<Boolean> result=new Result<>();
        result.setData(generalConfigDispatch.batchUpdate(models));
        result.setTime(System.currentTimeMillis()-now);
        return result;
    }

    /**
     * 对象修改
     * @param model 修改的对象
     * @return
     */
    @PostMapping("update")
    public Result<Boolean> update(@RequestBody GeneralConfig model)  {
        long now=System.currentTimeMillis();
        Result<Boolean> result=new Result<>();
        result.setData(generalConfigDispatch.update(model));
        result.setTime(System.currentTimeMillis()-now);
        return result;
    }

    /**
     * 根据对象列表查询
     * @param model 查询条件
     * @return
     */
    @PostMapping("findByModel")
    public Result<List<GeneralConfig>> findByModel(@RequestBody GeneralConfig model)  {
        long now=System.currentTimeMillis();
        Result<List<GeneralConfig>> result=new Result<>();
        result.setData(generalConfigDispatch.findByModel(model));
        result.setTime(System.currentTimeMillis()-now);
        return result;
    }

    /**
     * 根据主键查询对象
     * @param id 查询的对象主键
     * @return
     */
    @GetMapping("findByPk")
    public Result<GeneralConfig> findByPk(@RequestParam("id") Long id)  {
        long now=System.currentTimeMillis();
        Result<GeneralConfig> result=new Result<>();
        result.setData(generalConfigDispatch.findByPk(id));
        result.setTime(System.currentTimeMillis()-now);
        return result;
    }

    /**
     * 分页查询
     * @param pageRequest 分页查询条件
     * @return
     */
    @PostMapping("findPage")
    public Result<Page<SysConfigVo>> findPage(@RequestBody  PageRequest<GeneralConfig> pageRequest)  {
        long now=System.currentTimeMillis();
        Result<Page<SysConfigVo>> result=new Result<>();
        result.setData(generalConfigDispatch.findPage(pageRequest.getPage(), pageRequest.getModel()));
        result.setTime(System.currentTimeMillis()-now);
        return result;
    }

    /**
     * 根据主键删除对象
     * @param ids 删除的对象主键
     * @return
     */
    @GetMapping("remove")
    public Result<Boolean> remove(@RequestParam("ids") List<Long> ids)  {
        long now=System.currentTimeMillis();
        Result<Boolean> result=new Result<>();
        result.setData(generalConfigDispatch.remove(ids));
        result.setTime(System.currentTimeMillis()-now);
        return result;
    }

    /**
     * 获取系统配置
     * @return result
     */
    @GetMapping("/getSystemConfig")
    public Result<String> getSystemConfigs(String type) {
        long now = System.currentTimeMillis();
        Result<String> result = new Result<>();
        result.setData(generalConfigDispatch.getSystemConfig(type));
        result.setTime(System.currentTimeMillis() - now);
        return result;
    }

    /**
     * 批量获取系统配置
     * @return result
     */
    @PostMapping("/getSystemConfig")
        public Result<List<String>> getSystemConfigs(@RequestBody List<String> types) {
        long now = System.currentTimeMillis();
        Result<List<String>> result = new Result<>();
        result.setData(generalConfigDispatch.getSystemConfigs(types));
        result.setTime(System.currentTimeMillis() - now);
        return result;
    }

    /**
     * 获取用户配置
     * @return result
     */
    @PostMapping("/getUserConfig")
    public Result<Object> getUserConfig(@RequestBody UserConfigDto model) {
        long now = System.currentTimeMillis();
        Result<Object> result = new Result<>();
        result.setData(generalConfigDispatch.getUserConfig(model));
        result.setTime(System.currentTimeMillis() - now);
        return result;
    }

    /**
     * 保存用户配置
     */
    @PostMapping("/setUserConfig")
    public Result<Boolean> setUserConfig(@RequestBody UserConfigDto model) {
        long now = System.currentTimeMillis();
        Result<Boolean> result = new Result<>();
        result.setData(generalConfigDispatch.setUserConfig(model));
        result.setTime(System.currentTimeMillis() - now);
        return result;
    }

}